package com.yanqu.road.dao.impl.activity.suoyao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suoyao.SuoYaoChooseRewardData;
import com.yanqu.road.entity.activity.suoyao.SuoYaoUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SuoYaoUserDataDaoImpl extends TempDataObjectDao<SuoYaoUserData> {

    public static String getSuoYaoUserDataSql = "select * from t_u_activity_suoyao_user_data where activityId=? and userId=?";

    public SuoYaoUserData getSuoYaoUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSuoYaoUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSuoYaoUserDataSql = "update `t_u_activity_suoyao_user_data` set floorNo=?,rewardIdList=?,score=?,swordValue=?," +
            "drawBoxMap=?,totalTimes=?,commonTimes=?,lowHideTimes=?,round=?,hasGetRewardData=?,chooseRewardData=?,hasGetBookRewardList=?," +
            "hasGetSelectedData=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(SuoYaoUserData suoYaoUserData) {
        suoYaoUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getFloorNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getRewardIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getSwordValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getDrawBoxMap())));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getLowHideTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getHasGetRewardData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getChooseRewardData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getHasGetBookRewardList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getHasGetSelectedData())));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, suoYaoUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSuoYaoUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSuoYaoUserDataSql = "insert into `t_u_activity_suoyao_user_data`(activityId,userId,floorNo,rewardIdList,score," +
            "swordValue,drawBoxMap,totalTimes,commonTimes,lowHideTimes,round,hasGetRewardData,chooseRewardData,hasGetBookRewardList," +
            "hasGetSelectedData) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(SuoYaoUserData suoYaoUserData) {
        suoYaoUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, suoYaoUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getFloorNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getRewardIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getSwordValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getDrawBoxMap())));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getLowHideTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, suoYaoUserData.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getHasGetRewardData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getChooseRewardData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getHasGetBookRewardList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(suoYaoUserData.getHasGetSelectedData())));
        if (getDb().executeNoneQuery(insertSuoYaoUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected SuoYaoUserData parseData(ResultSet rs) throws SQLException {
        SuoYaoUserData suoYaoUserData = new SuoYaoUserData();
        suoYaoUserData.setActivityId(rs.getInt("activityId"));
        suoYaoUserData.setUserId(rs.getLong("userId"));
        suoYaoUserData.setFloorNo(rs.getInt("floorNo"));
        suoYaoUserData.setRewardIdList(JSON.parseObject(rs.getString("rewardIdList"), new TypeReference<List<Integer>>() {
        }));
        suoYaoUserData.setScore(rs.getInt("score"));
        suoYaoUserData.setSwordValue(rs.getInt("swordValue"));
        suoYaoUserData.setDrawBoxMap(JSON.parseObject(rs.getString("drawBoxMap"), new TypeReference<Map<Integer, Integer>>() {
        }));
        suoYaoUserData.setTotalTimes(rs.getInt("totalTimes"));
        suoYaoUserData.setCommonTimes(rs.getInt("commonTimes"));
        suoYaoUserData.setLowHideTimes(rs.getInt("lowHideTimes"));
        suoYaoUserData.setRound(rs.getInt("round"));
        suoYaoUserData.setHasGetRewardData(JSON.parseObject(rs.getString("hasGetRewardData"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.suoyao.SuoYaoHasGetRewardData>>() {}));
        suoYaoUserData.setChooseRewardData(JSON.parseObject(rs.getString("chooseRewardData"), SuoYaoChooseRewardData.class));
        suoYaoUserData.setHasGetBookRewardList(JSON.parseObject(rs.getString("hasGetBookRewardList"), new TypeReference<java.util.List<java.lang.Integer>>() {})); suoYaoUserData.setHasGetRewardData(JSON.parseObject(rs.getString("hasGetRewardData"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.suoyao.SuoYaoHasGetRewardData>>() {}));
        suoYaoUserData.setHasGetSelectedData(JSON.parseObject(rs.getString("hasGetSelectedData"), new TypeReference<java.util.Map<java.lang.Integer,
                com.yanqu.road.entity.activity.suoyao.SuoYaoHasGetRewardData>>() {}));


        return suoYaoUserData;
    }

}
